package com.faxsun.web.processor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor;
import org.thymeleaf.standard.expression.Expression;
import org.thymeleaf.standard.expression.StandardExpressions;

public class StaticResourceUrlRewriteProcessor extends
		AbstractAttributeModifierAttrProcessor {

	public StaticResourceUrlRewriteProcessor() {
		this("src");
	}

	protected StaticResourceUrlRewriteProcessor(final String attributeName) {
		super(attributeName);
	}

	@SuppressWarnings("unused")
	@Override
	protected Map<String, String> getModifiedAttributeValues(
			Arguments arguments, Element element, String attributeName) {
		Map<String, String> attrs = new HashMap<String, String>();
        HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
        
        boolean secureRequest = true;
        String contextPath = "";

        if (request != null) {
            secureRequest = isRequestSecure(request);
            contextPath = request.getContextPath();
        }
        
        String elementValue = element.getAttributeValue(attributeName);

        if (elementValue.startsWith("/")) {
            elementValue = "@{ " + elementValue + " }";
        }
        Expression expression = (Expression) StandardExpressions.getExpressionParser(arguments.getConfiguration())
                .parseExpression(arguments.getConfiguration(), arguments, elementValue);
        String assetPath = (String) expression.execute(arguments.getConfiguration(), arguments);
        
        if(assetPath.contains("www.faxsun.com/faxsun-info/upload")){
        	assetPath=assetPath.replace("www.faxsun.com/faxsun-info", "image.faxsun.com");
        }
        else if(assetPath.contains("www.faxsun.com/faxsun")){
        	assetPath=assetPath.replace("www.faxsun.com/faxsun", "static.faxsun.com/faxsun");
        }
        else if(assetPath.startsWith("/faxsun-info/upload")){
        	assetPath=assetPath.replace("/faxsun-info", "//image.faxsun.com");
        }
        
        attrs.put("src", assetPath);
        
        return attrs;
	}

	@Override
	protected ModificationType getModificationType(Arguments arguments,
			Element element, String attributeName, String newAttributeName) {

		return ModificationType.SUBSTITUTION;
	}

	@Override
	protected boolean removeAttributeIfEmpty(Arguments arguments,
			Element element, String attributeName, String newAttributeName) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected boolean recomputeProcessorsAfterExecution(Arguments arguments,
			Element element, String attributeName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getPrecedence() {
		// TODO Auto-generated method stub
		return 1000;
	}

	/**
	 * @return true if the current request.scheme = HTTPS or if the
	 *         request.isSecure value is true.
	 */
	protected boolean isRequestSecure(HttpServletRequest request) {
		return ("HTTPS".equalsIgnoreCase(request.getScheme()) || request
				.isSecure());
	}
}
